#ifndef MY_MAIN
#include "../inc/main.h"
#include "../inc/tcpServer.h"
#include "../inc/threadPool.h"
#include "../inc/config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <sys/epoll.h>

FILE *g_logFileStream;

int main(int argc, char **argv) {
    if (argc != 2) {
        fprintf(stderr, "args error!\n");
        exit(-1);
    }

    g_logFileStream = fopen("./server.log", "a+");

    // 获取服务端配置
    config_t config;
    memset(&config, 0, sizeof config);
    if (0 != getConfig(argv[1], &config)) {
        return -1;
    }

    // 创建并启动线程池
    int threadCount = atoi(config.threadCount);
    printk("thread count = %d\n", threadCount);
    threadPool_t *pThreadPool = NULL;
    threadPoolInit(threadCount, &pThreadPool);
    threadPoolStart(pThreadPool);

    // 获取套接口对应的文件描述符
    int serverSocketFileDescribe = 0;
    printk("ip = %s, port = %s\n", config.ip, config.port);
    getSocketFileDescribe(config.ip, config.port, &serverSocketFileDescribe);

    // 获取epoll对应的文件描述符
    int epollFileDescribe = epoll_create(1);

    // 开始服务
    tcpServer(serverSocketFileDescribe, epollFileDescribe, pThreadPool);

    return 0;
}

char *getTimeString(void) {
    static char timeString[40];
    memset(timeString, 0, sizeof timeString);
    time_t timeNow = time(NULL);
    struct tm *tmNow = localtime(&timeNow);
    sprintf(timeString, "[%4d/%02d/%02d %02d:%02d:%02d]", tmNow->tm_year + 1900, tmNow->tm_mon, tmNow->tm_mday,
        tmNow->tm_hour, tmNow->tm_min, tmNow->tm_sec);
    return timeString;
}

#endif